; ATAPI Interface for POSx86
;=========================================================;
; Send Atapi Packet                              01/06/06 ;
;---------------------------------------------------------;
; Dex4u OS V0.01                                          ;
; by Craig Bamford(Dex).                                  ;
;                                                         ;
;   Input:                                                ;
;          esi, points to 12 byte packet.                 ;
;                                                         ;
;=========================================================;


Send_Atapi_Packet:
        xor   dx,dx
        mov   bx,0xffff
        mov   cx,bx
        mov   dx,0x0177                                 ; Read Status = 7 + the base = 177
StatusReg1_1:                                  
        in    al,dx                                     ; Mov data from port to al
        and   al,0x80                                   ; Check bit 7(busy bit) ,loop if not 0
        jz    WriteCommand1_1                           ; Jump to WriteCommand1_1 if al bit7 =0
        loop  StatusReg1_1                              ; If al bit 7 =1,loop StatusReg1_1
        ret
WriteCommand1_1:                               
        dec   dx 
        xor   ax,ax                                   ; Bit 4 ,0 to select primary drive, 1 to select secondary drive
        out   dx,al                                     ; Write al to port (register)
        mov   cx,bx                                   ; Mov cx, number of loops(bx=0xffff)
        inc   dx  
StatusReg2_1:                                  
        in    al,dx                                     ; Mov data from port to al
        and   al,0x80                                   ; Check bit 7(busy bit) ,loop if not 0
        jz    WriteCommand2_1                           ; Jump to WriteCommand2_1 if al bit7 =0
        loop  StatusReg2_1                              ; If al bit 7 =1,loop StatusReg2_1
DeviceBusy_1:                                           ; time out:-(
        
        ret                                   

 ;----------------------------------------------------;
 ; Write Command                                      ;
 ;----------------------------------------------------; 

WriteCommand2_1:                               
              
        mov   al,0xA0                                   ; Add the 8 bit code to al
        out   dx,al                                     ; Write al to port (register)
        mov   cx,bx                                   ; Mov cx, number of loops(bx=0xffff)
StatusReg3_1:                                 
        in    al,dx                                     ; Mov data from port to al
        test  al,0x80                                   ; Does bit 7 = 0 (busy bit)
        jnz   DrqErrorCheck1_1                          ; Jump to "DrqErrorCheck1_1" if bit7 is 1
        test  al,0x08                                   ; Does bit3 = 0 (DRQ bit)
        jnz   WriteCommandPacket1_1                     ; Jump to label if bit3 is 1,we can transfer data to port:-) 
DrqErrorCheck1_1:                              
        loop  StatusReg3_1                              ; Loop label
        ret
      
 ;----------------------------------------------------;
 ; Write Command Packet                               ;
 ;----------------------------------------------------; 

WriteCommandPacket1_1:                         
        mov   dx,0x0170                                 ; Read date/write = 0              
        mov   cx,6                                      ; Mov number of words to mov into cx
WritePacket1_1:                                
        lodsw                                           ; Mov a word from packet to ax, inc esi 2
        out   dx,ax                                     ; Write ax to port (register)
        loop  WritePacket1_1                            ; Loop label

        mov   cx,bx                                   ; Mov cx, number of loops
        mov   dx,0x0177                                 ; Read Status = 7 + the base = 177 
StatusReg4_1:                                 
        in    al,dx                                     ; Mov data from port to al
        test  al,0x80                                   ; Does bit 7 = 0 (busy bit)
        jnz   DrqErrorCheck2_1                          ; Jump to "DrqErrorCheck2_1" if bit7 is 1
        ret

 ;----------------------------------------------------;
 ; Drq Error Check                                    ;
 ;----------------------------------------------------; 

DrqErrorCheck2_1:                             
        push  cx                                       ; Save old cx
        mov   cx,bx                                   ; Mov cx, number of loops(bx=0xffff)
BusyDelay_1:                                            ; Label
        nop                                             ; Do a null operation(xchg ax,ax)
        nop                                             ; Do a null operation(xchg ax,ax)
        nop                                             ; Do a null operation(xchg ax,ax)
        nop                                             ; Do a null operation(xchg ax,ax)
        loop  BusyDelay_1                               ; Loop label
        pop   cx                                       ; Get old cx
        loop  StatusReg4_1                              ; Loop label
     
error_1:
        
        ret
;====================================================;
; Eject cd                                           ;
;====================================================;

eject_cd:
        pusha
        mov   byte[packet+4],0x2
        mov   byte[packet+1],0x1
        mov   byte[packet+0],0x1b
        mov   si,packet
        call  Send_Atapi_Packet
        popa
        ret

;====================================================;
; Close cd                                           ;
;====================================================;

close_cd:
        pusha
        mov   byte[packet+0],0x1b
        mov   byte[packet+1],0x1
        mov   byte[packet+4],0x3
        mov   si,packet
        call  Send_Atapi_Packet
        popa
        ret


 ;--------------------------------------------------------------------------------------------;
 ;                                           Data                                             ;
 ;--------------------------------------------------------------------------------------------;

packet:
db 0x0
db 0x0
db 0x0
db 0x0
db 0x0
db 0x0
db 0x0
db 0x0
db 0x0
db 0x0
db 0x0
db 0x0

